using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CardDamageManager : MonoBehaviour
{
    public static CardDamageManager instance;

    private void Awake()
    {
        if (instance != null)
            Destroy(this);
        instance = this;
    }

    public bool SubmitDamage(DamageInfo damageInfo)
    {
        bool is_killed_target = false;
        
        // BdPackage.BdUtils.log($"--- on SubmitDamage, damageInfo.damage: {damageInfo.damage}");

        BuffHandle creatorBuffHandle = damageInfo.creator?.GetComponent<BuffHandle>();
        BuffHandle targetBuffHandle = damageInfo.target?.GetComponent<BuffHandle>();

        if (creatorBuffHandle)
        {
            foreach (var buffInfo in creatorBuffHandle.buffList)
            {
                buffInfo.buffData.OnHit?.Apply(buffInfo, damageInfo);
            }
        }

        if (targetBuffHandle)
        {
            foreach (var buffInfo in targetBuffHandle.buffList)
            {
                buffInfo.buffData.OnBeHurt?.Apply(buffInfo, damageInfo);
            }

            var component = damageInfo.target.GetComponent<CardController>();

            if (component)
            {
                if (component.IsCanBeKill(damageInfo, is_first_run: true))
                {
                    foreach (var buffInfo in targetBuffHandle.buffList)
                    {
                        buffInfo.buffData.OnBeKill?.Apply(buffInfo, damageInfo);
                    }

                    if (component.IsCanBeKill(damageInfo, is_first_run: false))
                    {
                        if (creatorBuffHandle)
                        {
                            foreach (var buffInfo in creatorBuffHandle.buffList)
                            {
                                buffInfo.buffData.OnKill?.Apply(buffInfo, damageInfo);
                            }
                        }

                        is_killed_target = true;
                    }
                }
                component.be_attack_times += 1;
            }
        }

        return is_killed_target;
    }
}
